package com.yazi.mxz.admin.controller;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.annotation.Resource;

import com.yazi.mxz.common.controller.BaseController;
import com.yazi.mxz.common.entity.beans.Category;
import com.yazi.mxz.common.service.CategoryService;
import com.yazi.mxz.admin.jackson.filter.CategoryFilter;
import com.myframework.smhj.common.exception.MyException;
import com.myframework.smhj.hibernate.Searching;
import com.myframework.smhj.hibernate.SearchingFactory;
import com.myframework.smhj.hibernate.Sort;
import com.myframework.smhj.hibernate.Sorting;
import com.myframework.smhj.jackson.result.ListResult;
import com.myframework.smhj.jackson.result.ObjectResult;
import com.myframework.smhj.jackson.result.StatusResult;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller("categoryController")
@RequestMapping("/category")
public class CategoryController extends BaseController {
	private static Logger log = LoggerFactory.getLogger(CategoryController.class);

	@Resource
	CategoryService categoryService;
	
	@RequestMapping("/list")
	public String list() {
		log.debug("管理->进入分类页面");
		return "category_list";
	}

	@RequestMapping("/listWithSons/json")
	@ResponseBody
	public ListResult<?> listWithSonsJson(Category category) {
		log.debug("用户 -> 获取分类json数据");
		ListResult<Category> jsonResult = new ListResult<Category>();
		jsonResult.setFilter(CategoryFilter.getWithCategories());
		try {
			Sorting sorting = SearchingFactory.createSorting("sort", true);
			List<Category> list = categoryService.list(category, sorting);
			jsonResult.setResult(list);
			jsonResult.setOk();
		} catch (Exception e) {
			log.error("用户 -> 获取子分类列表异常:", e);
			jsonResult.setErrorMessage("加载失败!");
		}
		return jsonResult;
	}

	@RequestMapping("/listSons/json")
	@ResponseBody
	public ObjectResult<?> listSonsJson(Long catId) {
		log.debug("获取子分类列表:catId={}", catId);
		ObjectResult<Category> jsonResult = new ObjectResult<Category>();
		jsonResult.setFilter(CategoryFilter.getWithCategories());
		try {
			Category parent = categoryService.one(catId);
			jsonResult.setResult(parent);
			jsonResult.setOk();
		} catch (Exception e) {
			log.error("获取子分类列表异常:", e);
			jsonResult.setErrorMessage("加载失败!");
		}
		return jsonResult;
	}

	@RequestMapping("/listBrothers/json")
	@ResponseBody
	public ObjectResult<?> listBrothersJson(Long catId) {
		log.debug("获取兄弟分类列表:catId={}", catId);
		ObjectResult<Category> jsonResult = new ObjectResult<Category>();
		jsonResult.setFilter(CategoryFilter.getWithCategories());
		try {
			Category category = categoryService.one(catId);
			if (null != category.getCategory()) {
				jsonResult.setResult(category.getCategory());
				jsonResult.setOk();
			} else {
				return listTypeRootsJson(-1);
			}
		} catch (Exception e) {
			log.error("获取兄弟分类列表异常:", e);
			jsonResult.setErrorMessage("加载失败!");
		}
		return jsonResult;
	}

	@RequestMapping("/listTypeRoots/json")
	@ResponseBody
	public ObjectResult<?> listTypeRootsJson(Integer type) {
		log.debug("获取根分类列表:type={}", type);
		ObjectResult<Category> jsonResult = new ObjectResult<Category>();
		jsonResult.setFilter(CategoryFilter.getWithCategories());
		try {
			List<Sort> sorts = new ArrayList<Sort>();
			sorts.add(new Sort("sort", true));
			Sorting sorting = new Searching();
			sorting.setSorts(sorts);
			
			Category category4s = new Category();// category for search
			category4s.setDeep(0);
			if (null != type && type >= 0) {
				// 某类型根的子一级分类列表
				category4s.setType(type);
				List<Category> rootList = categoryService.list(category4s, sorting);
				jsonResult.setResult(rootList.get(0));
			} else {
				// 根列表
				List<Category> rootList = categoryService.list(category4s, sorting);
				Category interim = new Category();
				Set<Category> sets = new HashSet<Category>();
				sets.addAll(rootList);
				interim.setCategories(sets);
				jsonResult.setResult(interim);
			}
			jsonResult.setOk();
		} catch (Exception e) {
			log.error("获取根分类列表异常:", e);
			jsonResult.setErrorMessage("加载失败!");
		}
		return jsonResult;
	}

	@RequestMapping("/addOrEdit")
	public String addOrEdit() {
		log.debug("管理->进入分类添加/修改页面");
		return "category_addOrEdit";
	}

	@RequestMapping("/addOrEditDo/json")
	@ResponseBody
	public StatusResult addOrEditDoJson(Category category) {
		log.debug("管理->分类添加/修改");
		StatusResult jsonResult = new StatusResult();
		try {
			categoryService.addOrEdit(category);
			jsonResult.setOk();
		} catch (MyException e) {
			log.debug("管理->分类添加/修改失败[{}]:{}", e.getClass().getSimpleName(), e.getMessage());
			jsonResult.setFailMessage("操作失败");
		} catch (Exception e) {
			log.error("管理->分类添加/修改异常:", e);
			jsonResult.setErrorMessage(e.getMessage());
		}
		return jsonResult;
	}
	
	@RequestMapping("/addDo/json")
	@ResponseBody
	public ObjectResult<?> addDoJson(Category category) {
		log.debug("添加分类");
		ObjectResult<Category> jsonResult = new ObjectResult<Category>();
		jsonResult.setFilter(CategoryFilter.getWithCategories());
		try{
			category = categoryService.addOrEdit(category);
			jsonResult.setResult(category);
			jsonResult.setOk();
		} catch (MyException e) {
			log.debug("管理->分类添加/修改失败[{}]:{}", e.getClass().getSimpleName(), e.getMessage());
			jsonResult.setFailMessage("添加失败");
		} catch (Exception e) {
			log.error("添加分类异常:", e);
			jsonResult.setErrorMessage(e.getMessage());
		}
		return jsonResult;
	}
	
	@RequestMapping("/editDo/json")
	@ResponseBody
	public StatusResult editDoJson(Category category) {
		log.debug("修改分类");
		StatusResult jsonResult = new StatusResult();
		try{
			categoryService.addOrEdit(category);
			jsonResult.setOk();
		} catch (MyException e) {
			log.debug("管理->分类修改失败[{}]:{}", e.getClass().getSimpleName(), e.getMessage());
			jsonResult.setFailMessage("更新失败");
		} catch (Exception e) {
			log.error("修改分类异常:", e);
			jsonResult.setErrorMessage(e.getMessage());
		}
		return jsonResult;
	}
	
	@RequestMapping("/delDo/json")
	@ResponseBody
	public StatusResult delDoJson(long delId, long bakupId) {
		log.debug("删除分类");
		StatusResult jsonResult = new StatusResult();
		try{
			categoryService.removeByAdmin(delId, bakupId);
			jsonResult.setOk();
		} catch (Exception e) {
			log.error("删除分类异常:", e);
			jsonResult.setErrorMessage(e.getMessage());
		}
		return jsonResult;
	}
	
}
